<template>
	<view class="container">
		<use-navbar ref="navbar" title="开票记录"></use-navbar>

		<view class="pos-f w-full navbar-area bg-drak" :style="{ top: navbarHeight + 'px' }">
			<view class="state-area dflex-a">
				<view v-for="(item, index) in navList" :key="index" :class="{ active: tabCurrentIndex === index }"
					class="nav-item dflex-c pos-r fs padding-lr-lg h-full" @click="tabClick(index)">
					{{ item.state }}
					<text v-if="item.cnt > 0">({{ item.cnt }})</text>
				</view>
			</view>
		</view>

		<swiper :current="tabCurrentIndex" :disable-touch="true" class="swiper-box swiper-area h-full" duration="300"
			@change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content h-full" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<use-empty v-if="tabItem.datas.length === 0 && tabItem.loaded" e-style="round" e-type="cart"
						tip="暂无数据" btn-tip="去开票" height="78vh" :auto="false" @goto="toOrder"></use-empty>
					<view class="padding-lr" v-for="(item, index) in tabItem.datas" :key="index">
						<view class="item-area border-radius margin-top-sm bg-main padding">
							<view class="item dflex-b" @click="$api.toPage(`/sub-user/pages/invoice/invoice`)">
								<text class="ft-dark">发票抬头</text>
								<view class="dflex-e flex1 margin-left-xl">
									<view class="line-height-1">{{ item.invoice.name }}</view>
									<view class="iconfont iconjiantou-01 fs-sm ft-dark"></view>
								</view>
							</view>
							<view class="item dflex-b margin-top-xs">
								<text class="ft-dark">发票金额</text>
								<text class="">￥{{ item.order_amount / 100 }}</text>
							</view>
							<view class="item dflex-b margin-top-xs">
								<text class="ft-dark">发票类型</text>
								<text class="">{{ item.invoice.type == 1 ? '个人抬头发票' : '企业抬头发票' }}</text>
							</view>
							<view class="item dflex-b margin-top-xs">
								<text class="ft-dark">申请时间</text>
								<view class="dflex-e flex1 margin-left-xl">
									<text class="">{{ $api.format(item.create_time) }}</text>
								</view>
							</view>
							<view v-if="item.state == 2" class="item dflex-b margin-top-xs">
								<text class="ft-dark">拒绝原因</text>
								<text class="">{{ item.remark || '--' }}</text>
							</view>

							<view class="dflex-b margin-top-sm">
								<view>
									<text v-if="item.state == 0">待开票</text>
									<text v-if="item.state == 1">已开具</text>
									<text v-if="item.state == 2">拒绝开票</text>
								</view>
								<view class="dflex-e">
									<view v-if="item.state == 0" class="dflex">
										<button class="action-btn border-radius-big bg-main main-btn"
											@click="toOrderDetail(item)">查看订单</button>
									</view>

									<view v-if="item.state == 1" class="dflex">
										<button class="action-btn border-radius-big bg-main"
											@click="toOrderDetail(item)">查看订单</button>
										<button class="action-btn border-radius-big bg-main main-btn"
											@click="toPreview(item)">预览</button>
									</view>

									<view v-if="item.state == 2" class="dflex">
										<button class="action-btn border-radius-big bg-main main-btn"
											@click="toOrderDetail(item)">查看订单</button>
									</view>
								</view>
							</view>
						</view>
					</view>

					<!-- 上拉加载更多 -->
					<use-loadmore v-if="tabItem.datas.length > 0 && tabItem.loaded && tabItem.hasmore"
						:type="tabItem.loadingType"></use-loadmore>

					<view style="height: 200rpx;"></view>
				</scroll-view>
			</swiper-item>
		</swiper>

		<view class="pos-f btn-container padding-tb bg-drak">
			<view class="dflex-b margin-lr border-radius-big safe-area-inset-bottom-plus">
				<view class="tac padding-tb-sm flex1 bg-base" @click="toInvoice">发票抬头信息</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				tabCurrentIndex: 0,
				navData: {},
				navList: [{
						id: 0,
						state: '已开票',
						stateKey: 1,
						cnt: 0,
						loadingType: 'more',
						datas: []
					},
					{
						id: 1,
						state: '申请中',
						stateKey: 0,
						cnt: 0,
						loadingType: 'more',
						datas: []
					},
					{
						id: 2,
						state: '已拒绝',
						stateKey: 2,
						cnt: 0,
						loadingType: 'more',
						datas: []
					}
				],
				reqdata: {
					page: 1,
					rows: 10
				},
				scrollLeft: 0,
				navbarHeight: 0
			};
		},
		watch: {
			tabCurrentIndex(nv, ov) {

				const cur_nav = this.navList[nv];
				if (cur_nav.loaded) return;

				this.loadData('tab_change', 1);
			}
		},
		onLoad(options) {
			this.loadData('refresh');
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.loadData('refresh');
		},
		// 上拉加载更多
		onReachBottom() {
			this.loadData();
		},
		mounted() {
			this.navbarHeight = this.$refs.navbar.navbarHeight;
		},
		methods: {
			// 加载数据
			loadData(source = 'add', loading) {
				// 获取当前 nav
				let cur_nav = this.navList[this.tabCurrentIndex];

				if (cur_nav.loadingType === 'loading') {
					//防止重复加载
					return;
				}

				if (loading == 1 || source == 'refresh') {
					cur_nav.page = 1;
				}
				if (source.type) {
					source.type = source.type.toLowerCase();
				}
				if (source === 'add' || source.type == 'scrolltolower') {
					if (cur_nav.loadingType == 'nomore') {
						return;
					}
					cur_nav.loadingType = 'loading';
				} else {
					cur_nav.loadingType = 'more';
				}

				this.reqdata.state = cur_nav.stateKey;
				this.reqdata.page = cur_nav.page;

				this.$func.usemall.call('member/invoice', this.reqdata).then(res => {

					cur_nav.loaded = true;

					if (res.code === 200) {
						if (loading == 1 || source == 'refresh') {
							cur_nav.datas = [];
						}

						if (cur_nav.page == 1) {
							let _nav = {};
							for (let _state in res.datas.dynamic) {
								_nav = this.navList.find(x => x.state == _state);
								if (_nav && _nav.state) {
									_nav.cnt = res.datas.dynamic[_state];
								}
							}
						}

						if (res.datas.length > 0) {
							let __datas = [];
							res.datas.forEach(row => {
								row.end_time = this.$api.format(row.end_time, 'yyyy-MM-dd hh:mm');
								__datas.push(row);
							});

							cur_nav.datas = [...cur_nav.datas, ...__datas];

							if (res.datas.length >= this.reqdata.rows) {
								if (cur_nav.page == 1) {
									cur_nav.hasmore = true;
								}
								cur_nav.page++;
								cur_nav.loadingType = 'more';
							} else {
								cur_nav.loadingType = 'nomore';
							}
						} else {
							cur_nav.loadingType = 'nomore';
						}
					}

					if (loading == 1) {
						uni.hideLoading();
					} else if (source == 'refresh') {
						uni.stopPullDownRefresh();
					}

					this.navData = cur_nav;
				});

			},

			// swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
			},
			// 顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
			},
			toInvoice() {
				uni.navigateTo({
					url: '/sub-user/pages/invoice/invoice'
				})
			},
			toOrder() {
				uni.navigateTo({
					url: '/sub-user/pages/order/order'
				})
			},
			toOrderDetail(res) {
				uni.navigateTo({
					url: `/sub-user/pages/order/order-detail?order_id=${res.order_id}`
				})
			},
			toPreview(res) {
				uni.showLoading({
					title: '打开中'
				});
				uni.downloadFile({
					url: res.invoice_url,
					success: (file) => {
						var filePath = file.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function(res) {
								console.log('打开文档成功');
							},
							fail: (err) => {
								console.log('openDocument err', err);
								this.$api.alert(err.errMsg);
							}
						});
					},
					fail: (err) => {
						console.log('downloadFile err', err);
						this.$api.alert(err.errMsg);
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	page,
	.container {
		min-height: 100%;
		background: $page-color-base;
	}

	.container {
		padding-top: 7vh;
	}

	/* 优惠券状态区 */
	.navbar-area {
		top: 0;
		white-space: nowrap;

		.state-area {
			height: 7vh;
			box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
			z-index: 10;
		}

		.nav-item {
			flex: 1;

			&.active {
				&:after {
					content: '';
					position: absolute;
					left: 50%;
					transform: translate(-50%);
					bottom: 0;
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}

	}

	/* 优惠券轮播区 */
	.swiper-area {
		height: 93vh;
	}

	.item-area {
		font-size: 28rpx;
		position: relative;

		&:last-child {
			margin-bottom: 20rpx;
		}

		.border-line {
			border-bottom: 1px dotted #ededed;
		}

		/* 操作按钮 */
		.action-btn {
			min-width: 128rpx;
			height: inherit;
			line-height: inherit;
			margin: 0;
			margin-left: 20rpx;
			padding: 12rpx 26rpx;
			font-size: $font-sm + 2upx;
			color: $font-color-dark;
			/* #ifdef MP-QQ || MP-ALIPAY */
			border: 1px solid;
			/* #endif */

			&:after {
				border-radius: 100px;
			}

			&.main-btn {
				background: #fff9f9;
				color: $base-color;

				&:after {
					border-color: #f7bcc8;
				}
			}
		}
	}

	.btn-container {
		left: 20rpx;
		right: 20rpx;
		bottom: 0;
	}
</style>